package com.cinema;
import java.io.*;
import java.util.ArrayList;

public class FileUtil {
    public static <T> void saveToFile(ArrayList<T> list, String filePath) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
            oos.writeObject(list);
        } catch (IOException e) {
            System.out.println("保存文件失败：" + e.getMessage());
        }
    }

    public static <T> ArrayList<T> readFromFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return new ArrayList<>();
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
            return (ArrayList<T>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("读取文件失败：" + e.getMessage());
            return new ArrayList<>();
        }
    }
}
